class Solution(object):
    def findLongestWord(self, s, dictionary):
        """
        :type s: str
        :type dictionary: List[str]
        :rtype: str
        """
        dictionary.sort(key=lambda x: (- len(x), x))
        def judge(s1, s2):
            n1 = len(s1)
            n2 = len(s2)
            left = 0
            right = 0
            while left < n1 and right < n2:
                if s1[left] == s2[right]:
                    left += 1
                    right += 1
                else:
                    right += 1
            if left == n1:
                return True
            else:
                return False

        for word in dictionary:
            if judge(word, s):
                return word

        return ''


data = Solution()
s = "abpcplea"
dictionary = ["ale", "apple", "monkey", "plea"]
print(data.findLongestWord(s, dictionary))
